#include "Physics.h"

void  Physics::ApplyForces(D3DXVECTOR3 *pLinear, D3DXVECTOR3 *pAngular){
	mLinearForce = pLinear;
}

void Physics::CalcVelocity(double pElapsedTime){
	//X Velocity
	mVelocity->x += mAccel->x* pElapsedTime; //V = AT

	//Y Velocity
	mVelocity->y += mAccel->y* pElapsedTime; //V = AT

	//Z Velocity
	mVelocity->z += mAccel->z* pElapsedTime; //V = AT
}

void Physics::CalcDisplacement(double pElapsedTime){

	mDisplacement->x = 0.5f*mVelocity->x*pElapsedTime;
	mDisplacement->y = 0.5f*mVelocity->y*pElapsedTime;
	mDisplacement->z = 0.5f*mVelocity->z*pElapsedTime;
}

void Physics::CalcMomentum(double pElapsedTime){
	//M = m(t) + ma*dt; a = f/m
	mMomentum->x = mMass* (mVelocity->x)*pElapsedTime;
	mMomentum->y = mMass* (mVelocity->y)*pElapsedTime;
	mMomentum->z = mMass* (mVelocity->z)*pElapsedTime;
}

void Physics::CalcAcceleration(){
	mAccel->x += mLinearForce->x/mMass; //F = ma;
	mAccel->y += mLinearForce->y/mMass; //F = ma;
	mAccel->z += mLinearForce->z/mMass; //F = ma;
}

void Physics::Update(double pElapsedTime){
	CalcDisplacement(pElapsedTime);
	CalcMomentum(pElapsedTime);
	
}

void Physics::Finalize(double pElapsedTime){
	mModel->GetPos()->x += mDisplacement->x;

	mModel->GetPos()->y += mDisplacement->y;
	mModel->GetPos()->z += mDisplacement->z;

	CalcAcceleration();
	CalcVelocity(pElapsedTime);
	mLinearForce = new D3DXVECTOR3(0,0,0);
	mDisplacement->x = 0;
	mDisplacement->y = 0;
	mDisplacement->z = 0;

}

D3DXVECTOR3* Physics::GetDisplacement(){
	return mDisplacement;
}

D3DXVECTOR3* Physics::GetLinearForce(){
	return mLinearForce;
}

float Physics::GetMass(){
	return mMass;
}

bool Physics::GetMoveable(){
	return mMoveable;
}

void Physics::Release(){

}